const app = getApp();
var api = require('../../api.js');
var WxParse = require('../../wxParse/wxParse.js');

Page({
  data: {
    list:{
      url:"",
      img:"",
      lesson:"",
      time:"",
      day:"",
      place:"",
      price: 0,
      intro:"",
    },
    id: 0,  //url中的信息
    value: 1,  //数量
    judge: false,  //判断是否抵扣
    integral: 0,  //抵扣之道币
    integralMoney: 0,  //抵扣之人民币
    Allintegral: 0,  //抵扣之道币
    AllintegralMoney: 0,  //抵扣之人民币
    total:0,  //总价
    article:"",  //介绍
    isVideo: true,
    videoSrc: '',  //视频地址
  },
  onLoad: function (options) {

    app.bind();
    var that = this;
    var pages = getCurrentPages(); //获取加载的页面
    var currentPage = pages[pages.length-1]; //获取当前页面的对象
    var options = currentPage.options; //如果要获取url中所带的参数可以查看options
    that.setData({
      id: options.id
    })
    that.getCourseDetail();
    // var temp = WxParse.wxParse('article', 'html', that.data.article, that, 5);
    // that.setData({
    //   article: temp
    // })
  },
  onReady: function () {
  
  },
  onShow: function () {
  
  },
  onHide: function () {
    var url = app.getcurrentPageId();
    wx.setStorageSync("from_url", url);
  },
  onUnload: function () {
  
  },
  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

    app.bind();
    var that = this;
    var pages = getCurrentPages(); //获取加载的页面
    var currentPage = pages[pages.length-1]; //获取当前页面的对象
    var options = currentPage.options; //如果要获取url中所带的参数可以查看options
    that.setData({
      id: options.id
    })
    that.getCourseDetail();

    // 显示顶部刷新图标  
    wx.showNavigationBarLoading();

    // 隐藏导航栏加载框  
    wx.hideNavigationBarLoading();

    //停止下拉刷新
    wx.stopPullDownRefresh();
  },
  onReachBottom: function () {
  
  },
  onShareAppMessage: function () {
    var url = app.getcurrentPageId();

    // var pages = getCurrentPages()    //获取加载的页面
    // var currentPage = pages[pages.length - 1]    //获取当前页面的对象
    // console.log(currentPage);
    // var url = currentPage.route    //当前页面url
    // var id = currentPage.options.id;
    // url = url + "?id=" + id;

    var user_id = wx.getStorageSync('user_id');
    return {
      path: url + "&user_id=" + user_id,
      title: '得道1314'
    };
  },
  // 获取基本信息
  getCourseDetail: function () {
    var that = this;
    var access_token = wx.getStorageSync('access_token');
    var url = api.three.courseDetail;
    var id = that.data.id;
    console.log(access_token);
    console.log(url);
    wx.request({
      url:api.three.courseDetail,
      method: "get",
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      data: {
        access_token: access_token,
        id: that.data.id
      },
      success: function (e) {
        console.log(e);
        console.log(e.data.data);
        if (e.data.code == 0) {
          // var price = parseFloat(e.data.data.price);
          var total = e.data.data.price;
          var temp = WxParse.wxParse('article1', 'html', e.data.data.detail, that, 5);
          // var exists = e.data.data.video_info.VideoBase.Status;
          var exists = e.data.data.video_url;
          console.log("——————————————测试视频地址是否存在——————————————");
          console.log(exists);
          if (exists == '') {
            console.log("空");
            that.setData({
              isVideo: false,
            })
          } else {
            console.log("不空");
            that.setData({
              videoSrc: e.data.data.video_info.PlayInfoList.PlayInfo[1].PlayURL,
            })
          }
          that.setData({
            list: e.data.data,
            // total: e.data.data.price,
            // videoSrc: e.data.data.video_info.PlayInfoList.PlayInfo[1].PlayURL,
            total: total,
            integral: e.data.data.user.integral,
            integralMoney: e.data.data.user.integral2money,
            Allintegral: e.data.data.user.integral,
            AllintegralMoney: e.data.data.user.integral2money,
            article: temp,
          })
          that.maxIntegral(1); //改变抵扣值
          
          // console.log(that.data.videoSrc);
        } else {
          wx.navigateTo({
            url: '/pages/grant/grant'
          })
        }
      }
    });
  },  
  // 抵扣
  deductible: function(e) {
    var that = this;
    var judge = e.detail.value; //判断
    var price = that.data.list.price; //单价
    var number = that.data.value; //数量
    var integralMoney = that.data.integralMoney; //抵扣 
    if (judge == true) {
      that.setData({
        judge: true,
      })
      that.totalTrue(number);
    } else {
      that.setData({
        judge: false,
      })
      that.totalNo(number);
    }
  },
  // 减数量
  reduceValue: function(e) {
    var that = this;
    var price = that.data.list.price; //单价
    var number = that.data.value; //数量
    var integralMoney = that.data.integralMoney; //抵扣
    var judge = that.data.judge; //判断
    number --;
    if (number < 1) {
      number = 1;
    }
    that.maxIntegral(number);
    if (judge == true) {
      that.totalTrue(number);
    } else {
      that.totalNo(number);
    }
    that.setData({
      value: number,
    })  
  },
  // 加数量
  addValue: function(e) {
    var that = this;
    var price = that.data.list.price; //单价
    var number = that.data.value; //数量
    var integralMoney = that.data.integralMoney; //抵扣
    var judge = that.data.judge; //判断
    number ++;
    that.maxIntegral(number);
    if (judge == true) {
      that.totalTrue(number);
    } else {
      that.totalNo(number);
    }
    that.setData({
      value: number,
    })
  },
  // 手动修改数量
  inputChange: function (e) {
    console.log(e);
    var that = this;
    var number = e.detail.value; //数量
    var price = that.data.list.price; //单价
    var integralMoney = that.data.integralMoney; //抵扣
    var judge = that.data.judge; //判断
    if (number < 1) {
      number = 1;
    }
    that.maxIntegral(number);
    if (judge == true) {
      that.totalTrue(number);
    } else {
      that.totalNo(number);
    }
    that.setData({
      value: number,
    })
  },

  maxIntegral: function (number) { //改变抵扣值
    // console.log("——————————————改变抵扣值——————————————");
    var that = this;
    var price = that.data.list.price;
    var total = price * number;
    var integral = that.data.integral;
    var integralMoney = that.data.integralMoney;
    var Allintegral = that.data.Allintegral;  //总积分
    var AllintegralMoney = that.data.AllintegralMoney;  //总积分对应的人名币
    if (AllintegralMoney >= total) {
      integralMoney = parseFloat(total);
      integral = parseFloat(total * 10);
      that.setData({
        integral: integral,
        integralMoney: integralMoney,
      })
    } else {
      that.setData({
        integral: Allintegral,
        integralMoney: AllintegralMoney,
      })
    }
  },
  totalTrue: function (number) { //抵扣后改变总价
    console.log("——————————————抵扣后改变总价——————————————");
    console.log(number);
    var that = this;
    var price = that.data.list.price;
    var total = price * number;
    var Allintegral = that.data.Allintegral;  //总积分
    var AllintegralMoney = that.data.AllintegralMoney;  //总积分对应的人名币
    if (AllintegralMoney >= total) {
      that.totalEnough();
    } else {
      that.totalNotEnough(number,AllintegralMoney);
    }
  },
  totalNo: function (number) {  //不抵扣
    var that = this;
    var price = that.data.list.price;
    var total = price*number;
    // console.log(total);
    that.setData({
      total: total,
    })
  },
  totalEnough: function () {  //积分足够
    var that = this;
    that.setData({
      total: '0',
    })
  },
  totalNotEnough: function (number,integral) {  //积分不够
    var that = this;
    var price = that.data.list.price;
    var total = (price*number*100-integral*100)/100;
    console.log(total);
    that.setData({
      total: total,
    })
  },
  submit: function (e) { //结算
    var that = this;
    var access_token = wx.getStorageSync('access_token');
    var id = e.currentTarget.dataset.id;
    var num = that.data.value;
    var use_integral;
    if (that.data.judge == false) {
      use_integral = 0;
    } else {
      use_integral = that.data.integral;
    }
    console.log(id);
    console.log(num);
    console.log(use_integral);
    wx.request({
      url: api.courseOrder.submit,
      method: "post",
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      data: {
        access_token: access_token,
        id: id,
        num: num,
        use_integral: use_integral,
      },
      success: function (e) {
        console.log(e);
        console.log("————————————————————到已购————————————————————");
        if (e.data.code == 0) {
          if (e.data.msg == "订单支付成功") {
            console.log("————————————————————订单支付成功————————————————————");
            wx.switchTab({
              url: '/pages/bought/bought'
            })
          } else if (e.data.msg == "订单生成成功，微信支付下单成功") {
            console.log("————————————————————订单生成成功，微信支付下单成功————————————————————");
            //发起支付
            wx.requestPayment({
              timeStamp: e.data.data.timeStamp,
              nonceStr: e.data.data.nonceStr,
              package: e.data.data.package,
              signType: e.data.data.signType,
              paySign: e.data.data.paySign,
              success: function (e) {
                console.log(e)
                wx.switchTab({
                  url: '/pages/bought/bought'
                })
              },
              fail: function (e) {

              },
              complete: function (e) {

              }
            })
          }
        } else if (e.data.code == -1) {
          wx.reLaunch({
            url: '/pages/grant/grant',
          })
        }
      }
    });
  },
})